package com.yx.cloud.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.iot.common.utils.LogUtils;
import com.yx.cloud.R;
import com.yx.cloud.constant.Constants;
import com.yx.cloud.utils.StatusBarCompat;

public class BottomSettingView {
    private final static String TAG = "BottomSettingView";

    /**
     * 底部展开
     *
     * @param v
     */
    public static void animateOpen(NoSlidingViewPager viewPager, BottomLayoutView bv, View v, int height, View view, CheckBox mCBMainUnfold, Activity context,
                                   ImageView ivGreatManyIcon, ImageView ivLeftMenu, ImageView ivRightMenu, String yyy) {
        LogUtils.i("   animateOpen******  : " + yyy);
        //AppContext.get().setInterfaceIsSlide(true);//当底部打开时，设备列表有改变不进行刷新界面（删除设备回调除外）
        ivGreatManyIcon.setClickable(false);//主界面多设备图标点击事件
        ivLeftMenu.setClickable(false);//左侧菜单按钮
        ivRightMenu.setClickable(false);//右侧菜单按钮
        Constants.isButtomOpen = true;
        mCBMainUnfold.setChecked(true);
        viewPager.setNoScroll(true);
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0, height);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //动画结束，设置背景色 background_color transparent2
                view.setBackgroundColor(context.getResources().getColor(R.color.transparent2));
            }

            @Override
            public void onAnimationStart(Animator animation) {
                bv.selectMode();
                bv.setSuctionWater();
                bv.roomSelect();
            }
        });
        animator.start();
    }

    /**
     * 底部关闭
     *
     * @param
     */
    public static void animateClose(NoSlidingViewPager viewPager, Activity context, BottomLayoutView bv, View view, View view2, boolean isShow, CheckBox mCBMainUnfold,
                                    ImageView ivGreatManyIcon, ImageView ivLeftMenu, ImageView ivRightMenu, String hehe) {
        LogUtils.i(hehe + " :  animateClose******  : " + isShow);
//        ivGreatManyIcon.setClickable(true);//主界面多设备图标点击事件
        ivLeftMenu.setClickable(true);//左侧菜单按钮
        ivRightMenu.setClickable(true);//右侧菜单按钮
        Constants.isButtomOpen = false;
        viewPager.setNoScroll(false);
        mCBMainUnfold.setChecked(false);
        int origHeight = view.getHeight();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(outMetrics);
        float density = outMetrics.density;
        // Log.d(TAG, outMetrics.heightPixels + "  Height**是否全屏** 分辨率**  " + density);
        int height = 0;
        if (isShow) {
            height = (int) (density * (90 + 20)/*底部最低条条高度*/)/* + navigationBarHeight*/;
        } else {
            height = 0;
        }

        ValueAnimator animator = createDropAnimator(view, origHeight, height);

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view2.setBackgroundColor(context.getResources().getColor(R.color.transparent));
//                bv.initializationData("dynamic", "close");
//                bv.setSuctionWater();
//                bv.roomSelect();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }

    private static ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
//                Log.d(TAG, "  createDropAnimator*************************  onAnimationUpdate");
                ViewGroup.LayoutParams layoutParams = null;
                int value = (int) arg0.getAnimatedValue();
                layoutParams = v.getLayoutParams();
                layoutParams.height = value;
//                Log.d(TAG, v + "  createDropAnimator*************************  value:" + value);
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }


    public static boolean isNavigationBarShow(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Display display = context.getWindowManager().getDefaultDisplay();
            Point size = new Point();
            Point realSize = new Point();
            display.getSize(size);
            display.getRealSize(realSize);
            return realSize.y != size.y;
        } else {
            boolean menu = ViewConfiguration.get(context).hasPermanentMenuKey();
            boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            if (menu || back) {
                return false;
            } else {
                return true;
            }
        }
    }


    private static int getNavigationMode(Activity context) {
        //0 : Navigation is displaying with 3 buttons
//        1 : Navigation is displaying with 2 button(Android P navigation mode)
//        2 : Full screen gesture(Gesture on android Q)
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("config_navBarInteractionMode", "integer", "android");
        if (resourceId > 0) {
//            Log.d(TAG, "  Height**fffff***vvvv *density ：  ");
            return resources.getInteger(resourceId);
        }
//        Log.d(TAG, resourceId + "  Height**fffff****density ：  " + density);
        return 0;
    }

    public static int getNavigationBarHeight(Activity activity) {
        if (!isNavigationBarShow(activity)) {
            return 0;
        }
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height",
                "dimen", "android");
        //获取NavigationBar的高度
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    public static int getSceenHeight(Activity activity) {
//        Log.d(TAG, "  Height******density 底部高度1 ：  " + getNavigationBarHeight(activity));
        return getNavigationBarHeight(activity);
    }

    public static int getDefaultDisplayHeight(Activity activity) {
//        Log.d(TAG, "  Height******density 屏幕高度2：  " + activity.getWindowManager().getDefaultDisplay().getHeight());
        return activity.getWindowManager().getDefaultDisplay().getHeight();
    }

}
